Micron Document
Fox's Git Mirrors

Node / RFnexus / code.git / files / mls.hh

Displaying Raw • Download

mls.hh master (5e719f46) Text, 798 B

/*
Maximum length sequence

Copyright 2020 Ahmet Inan <inan@aicodix.de>
*/

#pragma once

namespace CODE {

class MLS
{
static int hibit(unsigned n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n ^ (n >> 1);
}
int poly, test, reg;
public:
MLS(int poly = 0b100000000000000001001, int reg = 1) : poly(poly), test(hibit(poly)>>1), reg(reg) {}
void reset(int r = 1)
{
reg = r;
}
int length()
{
return hibit(poly) - 1;
}
bool bad(int r = 1)
{
reg = r;
int len = length();
for (int i = 1; i < len; ++i) {
(*this)();
if (reg == r)
return true;
}
(*this)();
return reg != r;
}
int next()
{
(*this)();
return reg;
}
bool operator()()
{
bool fb = reg & test;
reg <<= 1;
reg ^= fb * poly;
return fb;
}
};

}


Served by rngit 1.4.1 - Generated in 0.02s